package JasPerl::JSTL::Fmt::SetBundleTag;

use 5.010;
use strict;
use warnings;

use parent qw(JasPerl::TagExt::JspTag);

use JasPerl::JSTL::Core::Config qw(:fmt);
use JasPerl::JSTL::Fmt::LocalizationContext;

sub set_basename {
    $_[0]->{basename} = $_[1];
}

sub set_scope {
    $_[0]->{scope} = JasPerl::PageContext->get_scope_by_name($_[1]);
}

sub set_var {
    $_[0]->{var} = $_[1];
}

sub do_tag {
    my $self = shift;
    my $context = $self->get_jsp_context();
    my $basename = $self->{basename} // ''; # no default bundle
    my $bundle = JasPerl::JSTL::Fmt::LocalizationContext->get_bundle($context, $basename);

    if (defined $self->{var}) {
        $context->set_attribute($self->{var}, $bundle, $self->{scope});
    } else {
        JasPerl::JSTL::Core::Config->set($context, FMT_LOCALIZATION_CONTEXT, $bundle, $self->{scope});
    }
}

1;
